/**
 * 全局格式化unix时间戳方法，默认显示year和second。
 */
const formatTime = (date, y = true, s = true) => {
  // 10位转换13位时间戳
  if (date.toString().length == 10) {
    date *= 1000;
  }

  if (!(date instanceof Date)) {
    date = new Date(date);
  }

  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + (y ? (s ? [hour, minute, second] : [hour, minute]).map(formatNumber).join(':') : "");
}

/**
 * 距离今天已经多少天
 */
const sinceToday = (date, end = null) => {
  var dateBegin = new Date(date); //将-转化为/，使用new Date
  var dateEnd = end == null ? new Date() : new Date(end); //获取当前时间
  var dateDiff = dateEnd.getTime() - dateBegin.getTime(); //时间差的毫秒数
  var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
  var leave1 = dateDiff % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000)) //计算出小时数
  //计算相差分钟数
  var leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000)) //计算相差分钟数
  //计算相差秒数
  var leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
  var seconds = Math.round(leave3 / 1000);
  var timesString = '';

  if (dayDiff != 0) {
    timesString = dayDiff+'天前';
    //  timesString = dateBegin.getFullYear().toString() + "-" + (dateBegin.getMonth() + 1).toString() + "-"+ dateBegin.getDate().toString();
  } else if (dayDiff == 0 && hours != 0) {
    timesString = hours + '小时前';
  } else if (dayDiff == 0 && minutes != 0) {
    timesString = minutes + '分钟前';
  } else {
    timesString = '刚刚';
  }

  return timesString;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
//保存图片到相册
export const writePhotosAlbum = (successFun, failFun) => {
  wx.getSetting({
    success(res) {
      if (!res.authSetting['scope.writePhotosAlbum']) {
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success: function () {
            successFun && successFun()
          },
          fail: function (res) {
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: "小程序需要您的微信授权保存图片，是否重新授权？",
              showCancel: true,
              cancelText: "否",
              confirmText: "是",
              success: function (res2) {
                if (res2.confirm) { //用户点击确定'
                  wx.openSetting({
                    success: (res3) => {
                      if (res3.authSetting['scope.writePhotosAlbum']) {
                        //已授权
                        successFun && successFun()
                      } else {
                        failFun && failFun()
                      }
                    }
                  })
                } else {
                  failFun && failFun()
                }
              }
            });
          }
        })
      } else {
        successFun && successFun()
      }
    }
  })
}

/**
 * 深度对比两个对象是否一致
 * from: https://github.com/epoberezkin/fast-deep-equal
 * @param  {Object} a 对象a
 * @param  {Object} b 对象b
 * @return {Boolean}   是否相同
 */
/* eslint-disable */
export const equal = (a, b) => {
  if (a === b) return true;

  if (a && b && typeof a == 'object' && typeof b == 'object') {
    var arrA = Array.isArray(a),
      arrB = Array.isArray(b),
      i, length, key;

    if (arrA && arrB) {
      length = a.length;
      if (length != b.length) return false;
      for (i = length; i-- !== 0;)
        if (!equal(a[i], b[i])) return false;
      return true;
    }

    if (arrA != arrB) return false;

    var dateA = a instanceof Date,
      dateB = b instanceof Date;
    if (dateA != dateB) return false;
    if (dateA && dateB) return a.getTime() == b.getTime();

    var regexpA = a instanceof RegExp,
      regexpB = b instanceof RegExp;
    if (regexpA != regexpB) return false;
    if (regexpA && regexpB) return a.toString() == b.toString();

    var keys = Object.keys(a);
    length = keys.length;

    if (length !== Object.keys(b).length)
      return false;

    for (i = length; i-- !== 0;)
      if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;

    for (i = length; i-- !== 0;) {
      key = keys[i];
      if (!equal(a[key], b[key])) return false;
    }

    return true;
  }

  return a !== a && b !== b;
}
/**
 * 判断一个 object 是否为 空
 * @param {object} object
 */
export const isEmpty = (object) => {
  for (const i in object) {
    return false;
  }
  return true;
}

module.exports = {
  formatTime: formatTime,
  sinceToday: sinceToday
}